
// NSRefDataToolDoc.h : interface of the CNSRefDataToolDoc class
//


#pragma once

#include "..\NSRefDataLib\adodb.h"
#include "LogCtrl.h"

#define CONNECTION_TIMEOUT 5000

class RefSheet;
class CNSRefDataToolDoc : public CDocument
{
protected: // create from serialization only
	CNSRefDataToolDoc();
	DECLARE_DYNCREATE(CNSRefDataToolDoc)

// Attributes
public:
	CListCtrl& sheet_ctrl() { return sheet_ctrl_; }
	CListCtrl& log_ctrl() { return log_ctrl_; }

private:
	CListCtrl sheet_ctrl_;
	LogCtrl log_ctrl_;
	std::wstring filename_;
	ExcelLibrary::IFile* excel_file_;
	typedef std::map<std::wstring, RefSheet*> MAP_REF_SHEETS;
	MAP_REF_SHEETS ref_sheets_;
	ADODB adodb_;

	typedef std::map<std::string,std::string> MAP_CONVERSION_EXPORTS;
	MAP_CONVERSION_EXPORTS	m_mapConversions;
	
// Operations
public:
	void Initialize();

	bool Open(const wchar_t* path);
	void Close();
	void WriteLog(Log& log);
	void StartPatch();
	
private:
	bool CreateRefSheets();
	void DeleteRefSheets();

	bool ExportXML(RefSheet* ref_sheet);
	bool ExportXMLDocument(RefSheet* ref_sheet, TiXmlDocument& doc);

	bool VerifyTable(RefSheet* ref_sheet);
	bool TruncateTable(const wchar_t* name);
	bool InsertTable(const wchar_t* name, const char* xml_path);

private:
	void WriteLog(Log::Type type, wchar_t* log_msg, ...);

	bool ParseConfig();
	bool ConverseExportFile( std::string& in, std::string& out );

// Overrides
public:
	virtual BOOL OnNewDocument();
	virtual void Serialize(CArchive& ar);

// Implementation
public:
	virtual ~CNSRefDataToolDoc();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
	DECLARE_MESSAGE_MAP()
	void ClearRefSheets() 
	{
		throw std::exception("The method or operation is not implemented.");
	}
};


